#include <iostream>
#include <string>

#include "../buttonrpc/buttonrpc.hpp"
#include "utils.hpp"

int main()
{
    // 创建客户端
    buttonrpc client;
    client.as_client("127.0.0.1", 5555);

    //
    while (1)
    {
        TaskResponse reply;
        // 内部调用zmq_msg_recv从socket上接收消息，并将消息存储在msg参数
        reply = client.call<TaskResponse>("AssginTask").val();

        int state = reply.state;
        if (reply.mapTask >= 0 && state == 0)// map task
        {
            string filename = reply.curTask.filename;
            KeyValue kv = getContent(filename);

            // 执行map
            vector<KeyValue> kvs = mapF(kv);
            writeOutMap(kvs, reply.curTask.mapId, reply.reduceTask);

            // 发送RPC给Coordinator告知已完成map任务的id
            client.call<void *>("TaskFin", reply.curTask.mapId);
        }
        else if (reply.mapTask >= 0 && state == 1)// reduce task    
        {
            //取得reduce任务，读取对应文件，shuffle后调用reduceFunc进行reduce处理
            vector<KeyValue> kvs = shuffle(reply.curTask.reduceId);
            
        }
        else if (state == 2)
        {
        }
        else
        {
        }
    }

    return 0;
}